package com.javafactroy.kunlong.bip.icp.message.base.codec;

import com.javafactroy.kunlong.bip.icp.message.base.iface.IMessage;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@Data
public class AppSingleDecoder {
    int ilen;
    boolean decodeOk;
    public int decodeLen(byte[] buf) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
        byteBuffer.order(ByteOrder.BIG_ENDIAN);
        return byteBuffer.getInt();
    }



    public void decode(byte[] buf) {
        setDecodeOk(false);
        if (buf.length >= 4) {
            setDecodeOk(true);
            ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
            setIlen(byteBuffer.getInt());
            int len = ilen - 4;
            byte[] dest = new byte[len];
            byteBuffer.get(dest);
            String[] ret = StringUtils.split(new String(dest), IMessage.LINE_SPLIT);

            if (ret.length > 1) {
                //setDecodeOk(true);
               // body.setBody(ret[1].getBytes());
               // header.decode(ret[0].getBytes());
            }

        }
    }

    public void decode(ByteBuffer buf) {
        buf.rewind();
        decode(buf.array());

    }

}
